package vn.info.vietnaminfo.sync;

import java.util.ArrayList;
import java.util.List;

import vn.info.vietnaminfo.HorizontalPager;
import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.adapter.DanhDauAdapter;
import vn.info.vietnaminfo.adapter.LoaiHinhKDAdapter;
import vn.info.vietnaminfo.adapter.ThanhPhoAdapter;
import vn.info.vietnaminfo.data.DSLoaiHinhKD;
import vn.info.vietnaminfo.data.DSThanhPho;
import vn.info.vietnaminfo.dialog.ShowDialog;
import vn.info.vietnaminfo.utli.FileUtil;
import vn.info.vietnaminfo.utli.GlobalVariable;
import vn.info.vietnaminfo.utli.JSONUtil;
import vn.info.vietnaminfo.utli.ShareDefaultData;
import vn.info.vietnaminfo.utli.Stuff;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class GetDataKhoiDongApp extends AsyncTask<String, Long, List<DSLoaiHinhKD>>
{
	private Context mContext;
	private ProgressDialog mProgressDialog;
	private ListView list64TinhThanh, listDanhDau;
	private LinearLayout horizontalPager, pagerIcon, layoutHome;
	private Animation aminShow;

	private List<DSThanhPho> listThanhPho;
	//private List<DSDiaDiem> listSortCuaHang;
	private List<DSLoaiHinhKD> listLoaiHinhKD = null;

	private ThanhPhoAdapter thanhPhoAdapter = null;
	private DanhDauAdapter danhDauAdapter = null;
	private String json = "";

	public GetDataKhoiDongApp(Context context, ListView _list64tt, ListView _listDanhDau, LinearLayout _horizontalPager, LinearLayout _pagerIcon, LinearLayout _layoutHome)
	{
		mContext 		= context;
		list64TinhThanh = _list64tt;
		listDanhDau 	= _listDanhDau;
		horizontalPager = _horizontalPager;
		pagerIcon 		= _pagerIcon;
		layoutHome 		= _layoutHome;
		aminShow		= AnimationUtils.loadAnimation(context, R.anim.push_show);
	}

	@Override
	protected void onPreExecute()
	{
		mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setMessage(mContext.getString(R.string.txt_dang_tai_data));
		mProgressDialog.show();
	}

	@Override
	protected List<DSLoaiHinhKD> doInBackground(String... params)
	{
		try
		{
			// Get danh sach 64 thanh pho
			listThanhPho = FileUtil.GetDSThanhPho(mContext);
			thanhPhoAdapter = new ThanhPhoAdapter(mContext, listThanhPho);
			//-----------------------------------------------------------------------------
	
	
			// Get danh sach loai hinh kinh doanh
			if(Stuff.isConnected(mContext))
			{
				listLoaiHinhKD = JSONUtil.getDSLoaiHinhKD(mContext, mContext.getString(R.string.http_loaihinh_kd));
			}
			else
			{
				json 	= 
						ShareDefaultData.getValueKeyString(mContext, ShareDefaultData.KEY_DS_LOAI_HINH_KINH_DOANH);
				Log.v("HungCX","json---->"+json);
				if(json == null)
					return null;
				else
					listLoaiHinhKD = JSONUtil.getDSLoaiHinhKDNotConnect(mContext, json);
			}
			return listLoaiHinhKD;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(List<DSLoaiHinhKD> result)
	{
		// Set danh sách 64 thành phố cho màn hình ch�?n thành phố
		if(thanhPhoAdapter != null)
		{
			list64TinhThanh.setAdapter(thanhPhoAdapter);
		}
		// Set danh sách các địa điểm đã xem
		if(danhDauAdapter != null)
		{
			listDanhDau.setAdapter(danhDauAdapter);
		}
		// Khởi tạo view switcher
		if(result != null && result.size() > 0)
		{
			HorizontalPager realViewSwitcher = new HorizontalPager(mContext);
			realViewSwitcher.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			horizontalPager.addView(realViewSwitcher);

			// Tính số icon hiển thị trên pager
			int maxIcon = 12;
			if(GlobalVariable.SCREEN_WIDTH == 480 && (GlobalVariable.SCREEN_HEIGHT == 800
					|| GlobalVariable.SCREEN_HEIGHT == 854)) { maxIcon = 16; }  

			// Tính số trang icon
			int totalPager = 0, balance = 0;
			if(listLoaiHinhKD.size() < maxIcon) { totalPager++; }
			else
			{
				totalPager = (listLoaiHinhKD.size() / maxIcon);
				balance = (listLoaiHinhKD.size() % maxIcon);
				if(balance > 0) { totalPager++; };
			}
			for(int pager = 0; pager < totalPager; pager++)
			{
				// Tạo danh sách icon theo pager
				int index = pager * maxIcon;
				List<DSLoaiHinhKD> listIcon = new ArrayList<DSLoaiHinhKD>();
				Drawable[] drawable = new Drawable[maxIcon];
				for(int icon = 0; icon < maxIcon; icon++)
				{
					if(index == listLoaiHinhKD.size()) { break; }
					drawable[icon] = GetDrawable(listLoaiHinhKD.get(index).getMaLoaiKD());
					listIcon.add(new DSLoaiHinhKD(listLoaiHinhKD.get(index).getMaLoaiKD(), listLoaiHinhKD.get(index).getTenLoaiKD()));
					index++;
				}

				// Tạo icon pager đang được select
				ImageView imageIcon = new ImageView(mContext);
				imageIcon.setId(pager);
				imageIcon.setBackgroundResource(R.xml.point);
				if(pager == 0) { imageIcon.setSelected(true); };
				pagerIcon.addView(imageIcon);

				GridView gridViewIcon = new GridView(mContext);
				gridViewIcon.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				gridViewIcon.setNumColumns(4);        	
				gridViewIcon.setSelector(mContext.getResources().getDrawable( R.drawable.bg_empty ));
				if((GlobalVariable.SCREEN_WIDTH == 480 && GlobalVariable.SCREEN_HEIGHT == 854)
						|| (GlobalVariable.SCREEN_WIDTH == 320 && GlobalVariable.SCREEN_HEIGHT == 480))
				{ 
					gridViewIcon.setPadding(0, 25, 0, 0);
				}        	
				LoaiHinhKDAdapter adapter = new LoaiHinhKDAdapter(mContext, drawable, listIcon);
				gridViewIcon.setAdapter(adapter);
				realViewSwitcher.addView(gridViewIcon);
			}
			realViewSwitcher.SetPagerIcon(pagerIcon);
			GlobalVariable.GET_DATA_CHO_APP = true;
			mProgressDialog.dismiss();
			layoutHome.setVisibility(View.VISIBLE);
			layoutHome.startAnimation(aminShow);
		}
		else
		{
			countdown = new MainCountDown(3000, 3000);
			countdown.start();
		}
	}

	private MainCountDown countdown;
	public class MainCountDown extends CountDownTimer
	{
		public MainCountDown(long millisInFuture, long countDownInterval)
		{
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish()
		{
			mProgressDialog.dismiss();
			ShowDialog dialog = new ShowDialog(mContext);
			dialog.ThongBaoCaiDatMang();
		}
		@Override
		public void onTick(long millisUntilFinished) {}
	}

	private Drawable GetDrawable(String filename)
	{
		try
		{
			int flagID = mContext.getResources().getIdentifier("drawable/" + filename, null, mContext.getPackageName());
			return mContext.getResources().getDrawable(flagID);
		}
		catch(Exception ex)
		{
			return null;
		}
	}
}